package com.itheima.d6_threadhomework0910.num4;

import java.util.List;
import java.util.Random;

public class EntranceTask implements Runnable {
    private int num;//入场人数
    private List<String> doubleColorBalls;//存放num张彩票


    public EntranceTask() {
    }

    //定义构造方法:给成员变量复制，确定入场人数，准备彩票
    public EntranceTask(int num, List<String> doubleColorBalls) {
        this.num = num;
        this.doubleColorBalls = doubleColorBalls;
    }

    /**
     * 获取
     *
     * @return num
     */
    public int getNum() {
        return num;
    }

    /**
     * 设置
     *
     * @param num
     */
    public void setNum(int num) {
        this.num = num;
    }

    /**
     * 获取
     *
     * @return doubleColorBalls
     */
    public List<String> getDoubleColorBalls() {
        return doubleColorBalls;
    }

    /**
     * 设置
     *
     * @param doubleColorBalls
     */
    public void setDoubleColorBalls(List<String> doubleColorBalls) {
        this.doubleColorBalls = doubleColorBalls;
    }


    @Override
    public void run() {
        Random r = new Random();
        //定义int变量count  记录每个入口进入的人数
        int count = 0;
        String threadName = Thread.currentThread().getName();

        while (true) {
            synchronized (this) {
                //所有人数已经到场，结束循环
                if (num <= 0) {
                    break;
                }
                //有人到场 随机抽取一个双色球（抽一个少一个）
                //产生一个随机索引
                int index = r.nextInt(doubleColorBalls.size());
                //获取一张双色球
                String doubleColorBallNumber = doubleColorBalls.remove(index);
                //打印输出
                System.out.println("编号为:" + (num--) + "的员工 从" + threadName + "入场！" +
                        "拿到的双色球彩票号码是:" + doubleColorBallNumber);
                count++;


            }
        }

        System.out.println("从" + threadName + "入场的员工总共: " + count + " 位员工");

    }
}
